﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Gui.Base.Context
{
    public sealed class ObjectContainer
    {
        /// <summary>
        /// 反射获取服务对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dllName"></param>
        /// <param name="serverName"></param>
        /// <returns></returns>
        public static T GetServerObject<T>(string serverName,
            string dllName = "RtPlatfromGui.UI.dll",
            string spaceName = "Siasun.AFC.RtPlatfromGui.UI.PageUI")
        {
            try
            {
                if (string.IsNullOrEmpty(dllName)) dllName = "RtPlatfromGui.UI.dll";
                if (string.IsNullOrEmpty(spaceName)) dllName = "Siasun.AFC.RtPlatfromGui.UI.PageUI";

                Assembly ass;
                Type type;
                object serverObject;
                string filePath = System.Environment.CurrentDirectory;
                if (System.IO.File.Exists(filePath + "\\" + dllName))
                {
                    //获取并加载DLL类库中的程序集
                    ass = Assembly.LoadFile(filePath + "\\" + dllName);
                    //显示并添加窗体到总的窗体页集合中                
                    type = ass.GetType(spaceName + "." + serverName);
                    //var page = Activator.CreateInstance(type, this);
                    serverObject = Activator.CreateInstance(type);
                    if (object.Equals(null, serverObject))
                    {
                        //return false;
                    }
                    if (serverObject is T)
                    {
                        return (T)serverObject;
                    }
                    else
                    {
                        //return false;
                    }
                }
                return default(T);
            }
            catch(Exception ex)
            {
                //LoggerInf.Error("反射接口服务失败!" + ex.Message.ToString());
                return default(T);
            }
        }

        /// <summary>
        /// 反射获取Facade对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="facadeName"></param>
        /// <returns></returns>
        public static T GetFacadeObject<T>(string facadeName)
        {
            try
            {
                Assembly ass;
                Type type;
                object serverObject;
                string filePath = System.Environment.CurrentDirectory;
                if (System.IO.File.Exists(filePath + "\\" + "TvmGuiTpl.Server.dll"))
                {
                    //获取并加载DLL类库中的程序集
                    ass = Assembly.LoadFile(filePath + "\\" + "TvmGuiTpl.Server.dll");
                    //显示并添加窗体到总的窗体页集合中                
                    type = ass.GetType("Siasun.AFC.TvmGuiTpl.Server.Application.Facade" + "." + facadeName);
                    //var page = Activator.CreateInstance(type, this);
                    serverObject = Activator.CreateInstance(type);
                    if (object.Equals(null, serverObject))
                    {
                        //return false;
                    }
                    if (serverObject is T)
                    {
                        return (T)serverObject;
                    }
                    else
                    {
                        //return false;
                    }
                }
                return default(T);
            }
            catch //(Exception ex)
            {
                //Siasun.AFC.Common.LoggerInf.Error("反射Facade接口服务失败!" + ex.Message.ToString());
                return default(T);
            }
        }
    }
}
